using System;
using System.Collections;
using System.Collections.Generic;
using Drill.TestSupport.TestDomains.SimpleDomain;
using NUnit.Framework;

namespace Drill.TestHelpers
{
    /// <summary>
    /// Abstract base class used to test implementations of <see cref="IDependencyResolver"/>.
    /// </summary>
    public abstract class DependencyResolverTestFixtureBase : object
    {
        private IDependencyResolver _dependencyResolverUnderTest;

        protected abstract IDependencyResolver CreateNewDependencyResolverUnderTest();
        protected abstract void Cleanup();

        [Category("UnitTest")]
        [Test]
        public void CanGetInstanceWithTypeParameter()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjA);
                ITestObjA obj = _dependencyResolverUnderTest.GetInstance(type) as ITestObjA;

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
				DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetInstanceWithTypeParameterWithIntegrationTestConfig()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjA);
                ITestObjA obj = _dependencyResolverUnderTest.GetInstance(type) as ITestObjA;

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetInstanceWithTypeAndKeyParameters()
        {
            const string key = "MyObjB";
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjB);
                ITestObjB obj = _dependencyResolverUnderTest.GetInstance(type, key) as ITestObjB;

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetInstanceWithTypeAndKeyParametersWithIntegrationTestConfig()
        {
            const string key = "MyObjB";
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjB);
                ITestObjB obj = _dependencyResolverUnderTest.GetInstance(type, key) as ITestObjB;

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetAllInstancesWithTypeParameter()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjB);
                IEnumerable objs = _dependencyResolverUnderTest.GetAllInstances(type);

                Assert.That(objs, Is.Not.Null);

                foreach (object obj in objs)
                {
                    ITestObjB testObjB = obj as ITestObjB;
                    Assert.That(testObjB, Is.Not.Null);
                }
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetAllInstancesWithTypeParameterWithIntegrationTestConfig()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjB);
                IEnumerable objs = _dependencyResolverUnderTest.GetAllInstances(type);

                Assert.That(objs, Is.Not.Null);

                foreach (object obj in objs)
                {
                    ITestObjB testObjB = obj as ITestObjB;
                    Assert.That(testObjB, Is.Not.Null);
                }
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetInstanceGeneric()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                ITestObjA obj = _dependencyResolverUnderTest.GetInstance<ITestObjA>();

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetInstanceGenericWithIntegrationTestConfig()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                ITestObjA obj = _dependencyResolverUnderTest.GetInstance<ITestObjA>();

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetInstanceGenericWithKeyParameter()
        {
            const string key = "MyObjB";
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                ITestObjB obj = _dependencyResolverUnderTest.GetInstance<ITestObjB>(key);

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetInstanceGenericWithKeyParameterWithIntegrationTestConfig()
        {
            const string key = "MyObjB";
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                ITestObjB obj = _dependencyResolverUnderTest.GetInstance<ITestObjB>(key);

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetAllInstancesGeneric()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                IEnumerable<ITestObjB> objs = _dependencyResolverUnderTest.GetAllInstances<ITestObjB>();

                Assert.That(objs, Is.Not.Null);

                foreach (ITestObjB obj in objs)
                {
                    Assert.That(obj, Is.Not.Null);
                }
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetAllInstancesGenericWithIntegrationTestConfig()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                IEnumerable<ITestObjB> objs = _dependencyResolverUnderTest.GetAllInstances<ITestObjB>();

                Assert.That(objs, Is.Not.Null);

                foreach (ITestObjB obj in objs)
                {
                    Assert.That(obj, Is.Not.Null);
                }
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }
    }
}